//
//  API.m
//  ivcs_ios_demo
//
//  Created by zealcomm user on 2020/10/22.
//  Copyright © 2020 zealcomm user. All rights reserved.
//

#import "API.h"

@interface API(){
    ZealcommNetWorkTool *_tool;
}

@property(nonatomic , copy)NSString *baseUrl;

@end

@implementation API

relizeSingleInstanceWithBlock(, ^{
    self->_tool = [ZealcommNetWorkTool sharedNetWorkTool];
})

-(void)loginWithUrl:(NSString *)url name:(NSString *)name password:(NSString *)pwd org:(NSString *)org complete:(completeBlock)complete{
    CallBackObject *callBackResult =[[CallBackObject alloc] init];
    NSDictionary *para = @{
        @"userName":name,
        @"pwd":pwd,
        @"role":@"customer",
        @"org":org
    };
    NSString *apiUrl = [NSString stringWithFormat:@"%@/ivcs/api/v1/auth/logon",url];
    [_tool POST:apiUrl parameters:para HeaderKeyValues:nil success:^(id  _Nonnull responseObject) {
        self.baseUrl = url;
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
        callBackResult.userInfo = result;
        callBackResult.code = 200;
        complete(callBackResult);
    } failure:^(NSError * _Nonnull error) {
        callBackResult.code = 101;//登录失败
        callBackResult.message = error.description;
        callBackResult.userInfo = error.userInfo;
        complete(callBackResult);
    }];
}

-(void)getScenarios:(NSString *)url onSuccess:(void (^)(id))onSuccess onFailure:(void (^)(NSError * _Nonnull))onFailure{
    // 旧的 url： /ivcs/api/v1/groups/select/options
    [_tool GET:[NSString stringWithFormat:@"%@/ivcs/api/v1/bizscenarios/select/options" , url] parameters:@{@"status":@"enabled"} HeaderKeyValues:@{@"x-access-token":self.token} success:^(id  _Nonnull responseObject) {
        NSDictionary *temp = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
        ZcoModelScenarioList *result = [[ZcoModelScenarioList alloc] initWithDictionary:temp];
        onSuccess(result.scenList);
    } failure:^(NSError * _Nonnull error) {
        onFailure(error);
    }];
}


-(void)uploadFile:(NSData *)fileData url:(NSString *)url fileName:(NSString *)fileName mimeType:(NSString *)mimeType onSuccess:(void (^)(id))onSuccess onFailure:(void (^)(NSError * _Nonnull))onFailure{
    [_tool Upload:url fileData:fileData fileName:fileName mimeType:mimeType parameters:nil HeaderKeyValues:nil success:^(id  _Nonnull responseObject) {
        onSuccess(responseObject);
    } failure:^(NSError * _Nonnull error) {
        onFailure(error);
    }];
}

-(void)uploadUserInfoWithSessionId:(NSString *)sessionId url:(NSString *)url userInfo:(NSMutableDictionary *)userInfo onSuccess:(void (^)(NSString *))onSuccess onFailure:(void (^)(NSError * _Nonnull))onFailure{
    [userInfo setObject:sessionId forKey:@"sessionId"];
    [_tool POST:url parameters:userInfo HeaderKeyValues:nil success:^(id  _Nonnull responseObject) {
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
        NSString *formId = [[result valueForKey:@"data"]  valueForKey:@"id"];
        onSuccess(formId);
    } failure:^(NSError * _Nonnull error) {
        onFailure(error);
    }];
}

-(void)uploadFormDataWithSessionId:(NSString *)sessionId url:(NSString *)url parameter:(NSMutableDictionary *)parameter onSuccess:(void (^)(id _Nonnull))onSuccess onFailure:(void (^)(NSError * _Nonnull))onFailure{
    [parameter setObject:sessionId forKey:@"sessionId"];
    [_tool POST:url parameters:parameter HeaderKeyValues:nil success:^(id  _Nonnull responseObject) {
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
        onSuccess(result);
    } failure:^(NSError * _Nonnull error) {
        onFailure(error);
    }];
}

- (void)confirmCollect:(NSDictionary *)data onSuccess:(void (^)(CallBackObject * _Nonnull))onSuccess onFailure:(void (^)(NSError * _Nonnull))onFailure{
    [_tool POST:[NSString stringWithFormat:@"%@/ivcs/api/v1/forminfo" , self.baseUrl] parameters:data HeaderKeyValues:@{@"x-access-token":self.token} success:^(id  _Nonnull responseObject) {
        NSDictionary *temp = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
        CallBackObject *callBackObject = [[CallBackObject alloc] init];
        callBackObject.code = [[temp valueForKey:@"code"] intValue];
        callBackObject.data = [temp valueForKey:@"data"];
        onSuccess(callBackObject);
    } failure:^(NSError * _Nonnull error) {
        onFailure(error);
    }];
}

- (void)updateCollectForm:(NSDictionary *)data onSuccess:(void (^)(id _Nonnull))onSuccess onFailure:(void (^)(NSError * _Nonnull))onFailure{
    [_tool POST:[NSString stringWithFormat:@"%@/ivcs/api/v1/sessions/form" , self.baseUrl] parameters:data HeaderKeyValues:@{@"x-access-token":self.token} success:^(id  _Nonnull responseObject) {
        NSDictionary *temp = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
        onSuccess(temp);
    } failure:^(NSError * _Nonnull error) {
        onFailure(error);
    }];
}


@end
